<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="关键字" prop="search">
        <el-input v-model="queryParams.search" placeholder="UID、昵称、电话号码" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 340px" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-button type="warning" size="mini" icon="el-icon-folder-add" @click="handleAdd('handleView')" v-hasPermi="['member:vip:updateCash']">会员冲金</el-button>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="list" @sort-change="changeTableSort" @row-dblclick="handleUpdate" stripe border>
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="创建时间" prop="createTime" width="160" align="center" :sortable="'custom'">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" prop="sysUserName" align="center" width="100" :sortable="'custom'" />
      <el-table-column label="UID" prop="userId" align="center" :sortable="'custom'" />
      <el-table-column label="昵称" prop="userNickName" align="center" width="100" />
      <el-table-column label="用户电话" prop="phone" align="center" width="150" />
      <el-table-column label="邀请码" prop="originalInviteCode" align="center" />
      <el-table-column label="现金账户" prop="cashAmount" align="center" width="120" :sortable="'custom'" />
      <el-table-column label="金积分账户" prop="goldAmount" width="120" align="center" :sortable="'custom'" />
      <el-table-column label="银积分账户" prop="sliverAmount" width="120" align="center" :sortable="'custom'" />
      <el-table-column label="佣金" prop="brokerageAmount" align="center" :sortable="'custom'" />
<!--      <el-table-column label="艺术指数" prop="artExponent" align="center" width="100" :sortable="'custom'" />-->
      <el-table-column label="说明" prop="explain" align="center" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="" @click="handleUpdate(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改角色配置对话框 -->
    <z-dialog :title="title" :visible.sync="open" :isShowConfirm="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="form.createTime" :disabled="true" />
        </el-form-item>
        <el-form-item label="UID" prop="userId">
          <el-input v-model.number="form.userId" :disabled="true" />
        </el-form-item>
        <el-form-item label="昵称" prop="userNickName">
          <el-input v-model="form.userNickName" :disabled="true" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="form.phone" :disabled="true" />
        </el-form-item>

      </el-form>
    </z-dialog>

    <div v-if="updateCashShow">
      <z-dialog :title="title" :visible.sync="updateCashShow" @confirm="submitForm" @cancel="cancel">
        <update-cash-view ref="handleView"></update-cash-view>
      </z-dialog>
    </div>

  </div>
</template>

<script>
import { appLogUserAmount, updateCash } from "@/api/vip/vip.js";
import updateCashView from "./updateCashView"

export default {
  name: "Gold", //会员冲金
  components: { updateCashView },
  data() {
    return {
      // 列表数据
      list: [],
      dateRange: [],
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      updateCashShow: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
        type: 'cash'  //类型 cash金额 vip会员
      },
      pageNum: 1,
      // 表单参数
      form: {},

      rules: {
        nickname: [{ required: true, message: "昵称不能为空" }],

      },
    };
  },
  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {
        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    dateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.createTimeStart = String(newV[0]);
          this.queryParams.createTimeEnd = String(newV[1]);
        }
      }
      else {
        this.queryParams.createTimeStart = undefined;
        this.queryParams.createTimeEnd = undefined;
      }
      this.getList();
    }
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      appLogUserAmount(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.list = response.data;
          this.total = response.total;
        }
        this.loading = false;
      });
    },

    changeTableSort(column) {
      this.queryParams.orderBy = column.prop;
      this.queryParams.isAsc = (column.order == 'descending' ? 0 : 1); //ascending == 升序 descending == 降序  0 倒,1正
      this.getList();
    },

    // 表单重置
    reset() {
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.createTimeStart = undefined;
      this.queryParams.createTimeEnd = undefined;
      this.dateRange = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 会员冲金按钮操作 */
    handleAdd(row) {
      let ref = this.$refs[row];
      if (ref) {
        ref.resetData();
      }
      this.reset();
      this.title = "会员冲金";
      this.updateCashShow = true;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "会员冲金查看";
      this.open = true;
      this.form = row;
    },

    /** 提交按钮 */
    submitForm(from) {
      let params = this.$refs.handleView.form;
      this.$refs.handleView.$refs.form.validate((valid) => {
        if (valid) {
          if (params.cashNum || params.goldNum || params.sliverNum || params.brokerageNum || params.inviteCode || params.artExponent) {
            updateCash(params).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("冲金成功");
                this.updateCashShow = false;
                this.isInit = true;
                this.getList();
              }
            });
          } else {
            if (!params.userId) {
              this.$modal.msgError('未选择UID');
            } else {
              this.$modal.msgError('数据没有改动');
            }
          }
        }
      });
    },

    cancel() {
      this.open = false;
      this.$refs.handleView.reset();
    }
  },
};
</script>
<style scoped>
</style>
